<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="关键字查询" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

  <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品编号" align="center" prop="productNo" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="库存数量" align="center" prop="quantity"  >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleStockQuantity(scope.row)"
          >{{ scope.row.quantity }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="可用库存量" align="center" prop="availableQuantity" />
    </el-table>

    <pagination
      v-show="total >0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table  :data="stockQuantityList" >
        <el-table-column label="仓库名称" align="center" prop="warehouseName" />
        <el-table-column label="库存数量" align="center" prop="quantity" >
        </el-table-column>
        <el-table-column label="可用库存量" align="center" prop="availableQuantity" />
        <el-table-column label="可用库存量" align="center" prop="availableQuantity" >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"

            >{{scope.row.availableQuantity}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock } from "@/api/module/stock";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      stockList: [],
      // 库存表格数据
      stockQuantityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productNo: null,
        productId: null,
        model: null,
        quantity: null,
        warehouseId: null,
        warehouseName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productName: null,
        productNo: null,
        productId: null,
        model: null,
        quantity: null,
        warehouseId: null,
        warehouseName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    },
    /** 库存按钮操作 */
    handleStockQuantity(row) {
      this.stockQuantityList = row.stockQuantityList;
      this.open = true;
      this.title = "库存";
    },
  }
};
</script>
